package factories;

import java.util.List;
import java.util.Random;

import logic.common.player.Player;
import logic.common.team.Fraction;
import logic.common.team.Team;
import logic.nodes.collision.DummyType;
import logic.nodes.nodeSettings.upgrades.UpgradableSettings;
import logic.ships.hunter.Hunter;
import logic.ships.hunter.HunterProperties;
import logic.weapons.Weapon;
import logic.weapons.WeaponType;

import com.jme.scene.Node;

import fileHandling.ModelImporter;
import fileHandling.SettingsLoader;

/**
 * Factory-Class for creating {@link Hunter}s. Provides methods to get properties of {@link Hunter}s and 
 * to create them.
 * 
 * @author Wasserleiche
 */
public class HunterFactory extends MovableShipFactory {
	
	public static Hunter createNewHunter(String shipName, Team team) {
		return createNewHunter(shipName, team, DummyType.None);
	}
			
	public static Hunter createNewHunter(String shipName, Team team, DummyType dummyType) {
		if(shipName == null || team == null) return null;
		
		Fraction fraction = team.getFraction();
		String fractionName = fraction.toString().toLowerCase();
		String hunterPath = "data/fractions/" + fractionName + "/ships/" + shipName + "/";
		
		HunterProperties properties = getHunterProperties(hunterPath);
		if(properties == null) return null;
		
		String modelPath = properties.getModelPath();
		Node hunterModel = ModelImporter.getModel(modelPath);
		if(hunterModel == null) return null;
		
		Hunter hunter = new Hunter(modelPath, shipName, hunterModel, properties, team, dummyType);		
		return hunter;
	}
	
	/**
	 * Creates a new {@link Hunter} of the given {@link Team}.
	 * @param team The {@link Team} the {@link Hunter} shall belong to.
	 * @return A new {@link Hunter} if no error occurred. null, else.
	 */
	public static Hunter createRandomHunter(Player bot) {
		Team team = bot.getTeam();
		
		Random r = new Random();
		String hunterName = team.getAvailableHunters().get(r.nextInt(team.getAvailableHunters().size()));
		
		Hunter hunter = createNewHunter(hunterName, team);
		if(hunter == null) return null;
		bot.spendMoney(hunter.getHunterProperties().getCosts());
		
		addRandomWeapons(bot, hunter, team.getEnergyWeaponNames(), r, hunter.getWeapontSlotsNumber(WeaponType.Energy));
		addRandomWeapons(bot, hunter, team.getHeatseakerNames(), r, hunter.getWeapontSlotsNumber(WeaponType.HeatSeeker));
		addRandomWeapons(bot, hunter, team.getMissileNames(), r, hunter.getWeapontSlotsNumber(WeaponType.Missile));
		return hunter;
	}
	
	public static void addRandomWeapons(Player bot, Hunter hunter, List<String> weaponNames, Random r, int maxSlots) {
		Team team = bot.getTeam();
		int weapons = weaponNames.size();
		for(int i = 0; i < maxSlots; i++) {
			String weaponName = weaponNames.get(r.nextInt(weapons));
			Weapon w = WeaponFactory.createHunterWeapon(team.getFraction(), weaponName, hunter);
			if(w == null) return;
			int costs = w.getWeaponProperties().getCosts();
			if(costs > bot.getMoney()) return;
			hunter.addWeapon(w);
			bot.spendMoney(costs);
		}
	}
	
	public static HunterProperties getHunterProperties(String shipPath) {
		UpgradableSettings settings = SettingsLoader.loadUpgradableSettings(shipPath + "properties.xml");
		return new HunterProperties(settings);
	}
}